package com.fighthouse.utils
{
	
	import com.fighthouse.entity.Idioma;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.geom.Rectangle;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.core.Application;
	import mx.events.FlexEvent;
	import mx.events.RSLEvent;
	import mx.managers.CursorManager;
	import mx.preloaders.DownloadProgressBar;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.RemoteObject;
	
	public class Preloader extends DownloadProgressBar
	{       
		private var preloadSprite:Sprite;
		
		private var menuItemService:RemoteObject;
		private var noticiaService:RemoteObject;
		private var atletaService:RemoteObject;
		private var bannerService:RemoteObject;
		private var videoService:RemoteObject;
		
		public function Preloader()
		{
			super();
			
			
			downloadingLabel = "Carregando Interface";
			initializingLabel = "Carregando Idiomas";
		}
		
		override protected function get labelRect():Rectangle
		{
			return new Rectangle(14, 17, 200, 16);
		}
		
		override public function set preloader(value:Sprite):void
		{
			preloadSprite = value;
			
			value.addEventListener(ProgressEvent.PROGRESS, progressHandler);	
			value.addEventListener(Event.COMPLETE, completeHandler);
			
			value.addEventListener(RSLEvent.RSL_PROGRESS, rslProgressHandler);
			value.addEventListener(RSLEvent.RSL_COMPLETE, rslCompleteHandler);
			value.addEventListener(RSLEvent.RSL_ERROR, rslErrorHandler);
			
			value.addEventListener(FlexEvent.INIT_PROGRESS, initProgressHandler);
			value.addEventListener(FlexEvent.INIT_COMPLETE, myInitCompleteHandler);
		}
		
		override protected function progressHandler(event:ProgressEvent):void
		{
			super.progressHandler(event);
			
			setProgress(event.bytesLoaded, event.bytesTotal + (event.bytesTotal / 3));
		}
		
		private function myInitCompleteHandler(event:FlexEvent):void
		{
			menuItemService = new RemoteObject("menuItemService");
			menuItemService.addEventListener(FaultEvent.FAULT, faultHandler);
			menuItemService.getList.addEventListener(ResultEvent.RESULT, menuItensListResultHandler);
			menuItemService.getListIdioma.addEventListener(ResultEvent.RESULT, idiomasListResultHandler);
			
			noticiaService = new RemoteObject("noticiaService");
			noticiaService.addEventListener(FaultEvent.FAULT, faultHandler);
			noticiaService.getList.addEventListener(ResultEvent.RESULT,noticiasResultHandler);
			
			atletaService = new RemoteObject("atletaService");
			atletaService.addEventListener(FaultEvent.FAULT, faultHandler);
			atletaService.getList.addEventListener(ResultEvent.RESULT,atletasResultHandler);
			
			bannerService = new RemoteObject("bannerService");
			bannerService.addEventListener(FaultEvent.FAULT, faultHandler);
			bannerService.getList.addEventListener(ResultEvent.RESULT,bannerResultHandler);
			
			videoService = new RemoteObject("videoService");
			videoService.addEventListener(FaultEvent.FAULT, faultHandler);
			videoService.getVideos.addEventListener(ResultEvent.RESULT,videosResultHandler);
			
			label = "Carregando idiomas";
			menuItemService.getListIdioma();
		}
		
		private function idiomasListResultHandler(event:ResultEvent):void
		{
			label = "Carregando itens de menu";
			Application.application.listaIdiomas = event.result as ArrayCollection;
			Application.application.selectedIdioma = ArrayCollection(event.result).getItemAt(0) as Idioma;
			menuItemService.getList();
			setProgress(75,100);
		}
		
		private function menuItensListResultHandler(event:ResultEvent):void
		{
			label = "Carregando noticias";
			Application.application.listaMenu = event.result as ArrayCollection;
			noticiaService.getList();
			setProgress(80,100);
		}
		
		private function noticiasResultHandler(event:ResultEvent):void
		{
			label = "Carregando atletas";
			Application.application.noticias = event.result as ArrayCollection;
			atletaService.getList();
			setProgress(85,100);
		}
		
		private function atletasResultHandler(event:ResultEvent):void{
			label = "Carregando banners";
			Application.application.atletas = event.result as ArrayCollection;
			bannerService.getList();
			setProgress(90,100);
		}
		private function bannerResultHandler(event:ResultEvent):void
		{
			label = "Carregando videos";
			setProgress(95,100);
			Application.application.banners = event.result as ArrayCollection;
			videoService.getVideos();
		}
		private function videosResultHandler(event:ResultEvent):void{
			label = "Carregado";
			setProgress(100,100);
			Application.application.videos = event.result as ArrayCollection;
			dispatchEvent(new Event(Event.COMPLETE));
		}
		
		private function faultHandler(event:FaultEvent):void
		{
			CursorManager.removeBusyCursor();
			Alert.show("Ocorreu um erro na inicialização. Por favor recarregue.", "Fight House");
		}
	}
}